<template>
  <a-modal :visible="show" title="接口测试验证" centered destroyOnClose :footer="null" width="960px"
           :confirmLoading="loading" @cancel="handleCancel"
           :bodyStyle="{'height':'400px','overflow-y':'auto','padding':'10px 20px'}">
    <a-row>
      <a-col :span="12" style="margin-bottom: 37px;">
        <a-form class="custom-form" :form="form" :label-col="{span:5}" :wrapper-col="{span:16}">
          <div class="online-title">接口信息</div>
          <a-form-item label="调用接口">
            <a-select @change="apiChange"
                      v-decorator="['apiInfoId',{initialValue:currentApi.id,rules:[{required:true,message:'请选择调用接口'}]}]">
              <a-select-option v-for="api in data.goodsApiConfList" :key="api.id" :value="api.id">
                {{ api.chineseName }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="appkey">
            <!--            placeholder="开发者密钥" v-decorator="['appkey',{initialValue:currentApi.tempKey,{rules:[{required:true,message:'请输入开发者密钥'}]}]"-->
            <a-input disabled v-model="currentApi.tempKey"/>
          </a-form-item>
          <div class="online-title">参数信息</div>
          <a-form-item v-for="param in currentApi.reqParamList" :key="param.id" :label="param.paramName">
            <a-input :maxLength="50" :placeholder="param.exampleValue"
                     v-decorator="[param.paramName,{initialValue: param.exampleValue,rules:[{required:param.must===1,message:'请输入'+param.remark}]}]"/>
            <div style="line-height: normal;font-size: 13px;color: #bfbfbf;">{{ param.remark }}</div>
          </a-form-item>
        </a-form>
      </a-col>
      <a-col :span="12">
        <div class="online-title">请求URL</div>
        <div class="online-card">{{ reqUrl }}</div>
        <div class="online-title" style="margin-top: 10px;">返回结果</div>
        <div class="online-card" style="min-height: 205px;">
          <json-viewer v-if="result" v-model="result" copyable sort :expand-depth="4" @copied="copied">
            <div slot="copy">
              复制
            </div>
          </json-viewer>
        </div>
      </a-col>
    </a-row>
    <a-row style="left: 0;right:0;position: absolute;bottom: 10px;">
      <a-col :span="12">
        <a-row style="text-align: center;">
          <a-col :span="20">
            <a-button type="primary" @click="sendReq">测试验证</a-button>
          </a-col>
        </a-row>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
export default {
  name: 'OnlineTest',
  props: {
    show: false,
    loading: false,
    data: {},
    initialApi: {},
    result: '',
    reqUrl: {
      type: String,
      default: ''
    },
  },
  watch: {
    initialApi: {
      handler (newVal) {
        this.currentApi = newVal
      },
      immediate: true,
      deep: true
    }
  },
  data () {
    return {
      form: this.$form.createForm(this, {name: 'form'}),
      currentApi: this.initialApi
    }
  },
  methods: {
    handleCancel () {
      this.form.resetFields()
      this.$emit('cancel')
    },
    apiChange (e) {
      console.log(e)
      this.currentApi = this.data.goodsApiConfList.filter(item => item.id === e)[0]
      this.currentApi.reqParamList = this.currentApi.goodsApiConfParamList.filter(item => item.type === 1)
      this.form.resetFields()
      this.form.setFieldsValue({'apiInfoId': e})
      this.$emit('reset')
    },
    sendReq () {
      this.form.validateFields((error, values) => {
        if (!error) {
          this.$emit('ok', {...values})
        }
      })
    },
    copied (e) {
      const text = e.text
      this.$message.success('复制成功')
    }
  }
}
</script>

<style scoped lang="scss">
.custom-form {
  > > > .ant-form-item {
    margin-bottom: 5px;
  }
}

.online-title {
  line-height: 20px;
  margin: 5px 0 10px 0;
  padding-left: 4px;
  position: relative;
  color: #333;

  &:before {
    content: ' ';
    position: absolute;
    top: 2px;
    left: -4px;
    width: 3px;
    border-radius: 3px;
    height: 16px;
    background-color: #0078FE;
  }
}

.online-card {
  border: 1px solid #0078FE;
  border-radius: 10px;
  box-shadow: 0 0 10px 0 rgba(0, 120, 254, 0.1);
  line-height: 20px;
  padding: 10px;
  min-height: 100px;

  > > > .jv-container .jv-code {
    padding: 0 !important;
  }
}
</style>

